package org.Component;

import org.Panel.CommentPanel;
import org.Tools.StringOP;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CommentBar extends JPanel {

    //头像
    JButton HeadPortrait = new JButton("头像");
    //用户名
    String username = "";
    //回复对象
    JLabel replyObject = new JLabel();
    //回复内容
    JLabel replyText = new JLabel();

    //回复日期
    JLabel replyTime = new JLabel();

    //回复按钮
    JLabel reply = new JLabel("回复");

    // 创建一个简单的线边框，颜色为黑色，粗细为1像素
    Border border = BorderFactory.createLineBorder(Color.BLACK, 1);

    //测量字符串宽度工具
    FontMetrics fm;

    //设置字体
    Font fon = new Font(null,0,20);

    //字符串行数
    int row = 0;

    //父控件的引用
    CommentPanel commentPanel;

    public CommentBar(String username,String replyText,String replyTime, CommentPanel cp) {

        initComponents(username+" : "+replyText,replyTime);
        this.username=username;
        commentPanel = cp;

    }

    public CommentBar(String username,String replyObject,String replyText,String replyTime, CommentPanel cp) {
        initComponents(username+"回复"+replyObject+" : "+replyText,replyTime);
        this.username=username;
        commentPanel = cp;

    }

    private void initComponents(String msg, String replyTime) {

        //设置初始属性
        setBounds(5,5,800,100);
        setLayout(null);
//        setBackground(Color.GRAY);
//        setBorder(border);
        //设置头像属性
        HeadPortrait.setBounds(10,10,30,30);

        //设置文本区字体
//        replyText.setBorder(border);

        //测量字符串宽度工具
        fm = replyText.getFontMetrics(replyText.getFont());
        //创建字符串操作类
        StringOP stringOP = new StringOP();
        //获取换行后的字符串
        String Pmsg = stringOP.LabelStr(msg,fm,490);
        //更新row
        row = stringOP.row;
        //修改JLabel文本
        replyText.setText(Pmsg);

        //调整界面大小
        int lh = row*fm.getHeight();
        if(row<1) lh=fm.getHeight();
        replyText.setBounds(70,10,550,lh);

        int bh =70+lh;
        if(bh<100) bh=100;
        setSize(600,bh);
        System.out.println(row);


        //日期属性
        this.replyTime.setText(replyTime);
        this.replyTime.setBounds(400,lh+20,150,40);

        //回复按钮
        reply.setBounds(560,lh+20,50,40);
        reply.addMouseListener(new MouseAdapter() {
            //鼠标移入标识
            int f=1;
            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    commentPanel.hint = "回复"+username+"：";
                    commentPanel.updateHintVisibility();
                    commentPanel.replyJudge = 1;
                    commentPanel.replyUser = username;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //改变鼠标移入标识
                f=1;
                //鼠标移动至文本中,改变鼠标指针样式
                reply.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                //将文字颜色改为蓝色
                reply.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                //当鼠标移出文本时,回复文字颜色黑色
                reply.setForeground(Color.BLACK);
            }
        });

        add(HeadPortrait);
        add(replyText);
        add(this.replyTime);
        add(reply);

    }

    private String LabelRow(String msg) {
        //字符串长度
        int len = msg.length();
        //指向字符串的指针
        int pt = 0;

        //处理后的字符串
        String Pmsg = "<html>";

        //每行文本的长度
        int w = 490;

        //处理字体换行
        while(pt<len) {
            String str = "";
            while(pt<len) {
                if(msg.charAt(pt)=='\n') {
                    pt++;
                    break;
                }
                str+= msg.charAt(pt);
                pt++;
                if(fm.stringWidth(str)>w) break;
            }
            Pmsg+=str+"<br>";
            row++;
            System.out.println(row);
            System.out.println(str);
        }

        Pmsg+="</html>";    //加上结尾标识
        return Pmsg;
    }

}
